package com.gxa.service.impl;

import com.gxa.common.JsonResult;
import com.gxa.common.R;
import com.gxa.dao.EmpDao;
import com.gxa.dao.impl.EmpDaoImpl;
import com.gxa.pojo.Department;
import com.gxa.pojo.Role;
import com.gxa.pojo.User;
import com.gxa.service.EmpService;
import com.gxa.utils.MD5Utils;

import java.sql.SQLException;
import java.util.List;

public class EmpServiceImpl implements EmpService {
    private EmpDao empDao = new EmpDaoImpl();
    @Override
    public JsonResult queryemplist(Integer currentpage, Integer pagesize) {
        JsonResult jsonResult = new JsonResult();
        try {
            Integer totalsize = empDao.countUser();
            Integer p1 = (currentpage-1)*pagesize;
            Integer p2 = pagesize;
            List<User> users = empDao.queryemplist(p1, p2);
            for (User user : users) {
                Integer role_id = user.getRole_id();
                Role roles = empDao.getRole(role_id);
                if (roles!=null){
                    Integer department_id = roles.getDepartment_id();
                    Department department = empDao.getDepartment(department_id);
                    roles.setDepartment(department);
                }
                user.setRole(roles);
            }
            if (users!=null){
                jsonResult.setCode(0);
                jsonResult.setMsg("查询成功！");
                jsonResult.setCount(totalsize);
                jsonResult.setData(users);
            }
        } catch (SQLException e) {
           e.printStackTrace();
            jsonResult.setCode(500);
            jsonResult.setMsg("系统开小差，请联系管理员！");
        }

        return jsonResult;
    }

    @Override
    public JsonResult queryEmpList(Integer currentPage, Integer pagesize, String username) {
        JsonResult jsonResult = new JsonResult();
        try {
            Integer p1 = (currentPage-1)*pagesize;
            Integer p2 = pagesize;
            Integer totalsize = empDao.countUser(username);
            List<User> userList = empDao.queryUserList(p1, p2, username);
            for (User user : userList) {
                Integer role_id = user.getRole_id();
                Role role = empDao.getRole(role_id);

                if (role!=null){
                    Integer department_id = role.getDepartment_id();
                    Department department = empDao.getDepartment(department_id);
                    role.setDepartment(department);
                }
                user.setRole(role);
            }

            if (userList!=null){
                jsonResult.setCode(0);
                jsonResult.setMsg("查询成功！");
                jsonResult.setCount(totalsize);
                jsonResult.setData(userList);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            e.printStackTrace();
            jsonResult.setCode(500);
            jsonResult.setMsg("系统开小差，请联系管理员！");
        }
        return jsonResult;
    }

    @Override
    public R addEmp(User user) {
        R r = new R();
        try {
            //初始密码
            String password = "000000";
            String initialPassword = MD5Utils.encrypt(password, MD5Utils.MD5_KEY);
            user.setInitialpassword(initialPassword);

            Role role = empDao.searchRole(user);
            Department department = empDao.searchDepartName(user.getDepartment_id());
            user.setDepartment_name(department.getName());
            user.setRole_id(role.getId());
            Integer integer = empDao.addEmp(user);
            if(integer>0){
                r.setCode(200);
                r.setMsg("添加成功！");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差，请联系管理员！");
        }
        return r;
    }

    @Override
    public R delUserById(String[] ids) {
                R r = new R();
            try {
                if(ids.length>0) {
                    for (String id : ids) {
                        empDao.delUser(id);
                    }
                }
                r.setCode(200);
                r.setMsg("删除成功!");
                } catch (SQLException e) {
                    e.printStackTrace();
                r.setCode(500);
                r.setMsg("系统开小差，请联系管理员！");
                }
        return r;
    }

    @Override
    public R updateUser(User user) {
        R r = new R();
        try {
            Role role = empDao.searchRole(user);
            System.out.println(role.getId());
            Department department = empDao.searchDepartName(user.getDepartment_id());
            user.setDepartment_name(department.getName());
            user.setRole_id(role.getId());
            Integer integer = empDao.updateUser(user);
            if(integer>0){
                r.setCode(200);
                r.setMsg("编辑成功！");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差，请联系管理员！");
        }
        return r;
    }

    /**
     * 启用/禁用
     * @param ids
     * @return
     */
    @Override
    public R enableDisableUser(String[] ids) {
        R r = new R();
        try {
            if (ids.length>0){
                for (String id : ids) {
                    User user = empDao.statusUser(id);
                    System.out.println(user.getStatus());
                    if (user.getStatus()==1){
                        empDao.disableUser(id);
                    }
                    if (user.getStatus()==2){
                        empDao.enableUser(id);
                    }
                }
            }
            r.setCode(200);
            r.setMsg("启用/禁用成功！");
        }catch (SQLException e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统开小差，请联系管理员！");
        }
        return r;
    }

    @Override
    public R editUserInfo(String[] params) {
        R r = new R();
        String username = params[0];
        String name_num = params[1];
        String oldpassword = params[2];
        String password = params[3];
        String repassword = params[4];
        System.out.println(params[0]);
        System.out.println(params[1]);
        System.out.println(params[2]);
        System.out.println(params[3]);
        System.out.println(params[4]);
        try {
            if(!repassword.equals(password)){
                r.setCode(500);
                r.setMsg("密码不一致！");
            }else {
                String newpassword = MD5Utils.encrypt(password, MD5Utils.MD5_KEY);
                String oldpass = MD5Utils.encrypt(oldpassword,MD5Utils.MD5_KEY);
                System.out.println(newpassword);
                Integer integer = empDao.editUserInfo(username, name_num, oldpass, newpassword);

                if (integer>0){
                    r.setCode(200);
                    r.setMsg("修改成功！");
                }else {
                    r.setCode(502);
                    r.setMsg("修改失败，请重试！");
                }
            }
        }catch (SQLException e){
            e.printStackTrace();
            r.setCode(501);
            r.setMsg("系统开小差，请联系管理员！");
        }
        return r;
    }


}
